#include <iostream>
#include <unistd.h>
#include <sys/epoll.h>
#include <string>
#include "socket.hpp"
#include <stdlib.h>

#define SIZE 128
#define NUM 64

void Usage(const std::string& proc)
{
    std::cout << "Usage: " << proc << " port" << std::endl;
}
int main(int argc, char* argv[])
{
    if(argc != 2)
    {
        Usage(argv[0]);
        exit(1);
    }
    //监听套接字
    int listen_sock = Sock::Socket();
    uint16_t port = atoi(argv[1]);
    Sock::Bind(listen_sock, port);
    Sock::Listen(listen_sock);

    //创建epoll模型
    int efd = epoll_create(SIZE);
    struct epoll_event ev;
    ev.data.fd = listen_sock;
    ev.events = EPOLLIN;
    int ret = epoll_ctl(efd, EPOLL_CTL_ADD, listen_sock, &ev);
    if(ret < 0)
    {
	std::cout << "epoll_ctl error" << std::endl;
	exit(2);
    }	    

    struct epoll_event evs[NUM];
    bool quit = false;
    while(!quit)
    {
        int ret = epoll_wait(efd, evs, NUM, 0);
	    std::cout << "epoll所关心的文件描述符数量 " << ret << std::endl;
        switch(ret)
        {
            case -1:
                std::cout << "epoll error " << std::endl;
                break;
            case 0:
                std::cout << "time out ..." << std::endl;
                break;
            default:
                for(int i = 0;i < ret;++i)
                {
                    int sock = evs[i].data.fd;
                    if(evs[i].events & EPOLLIN)
                    {
                        if(sock == listen_sock)
                        {
                            std::cout << "有新的连接到来了" << std::endl;
                            //来的是监听套接字
                            // int fd = Sock::Accept(sock);
                            // if(fd >= 0)
                            // {
                            //     struct epoll_event _ev;
                            //     _ev.data.fd = fd;
                            //     _ev.events = EPOLLIN;
                            //     epoll_ctl(efd, EPOLL_CTL_ADD, fd, &_ev);
                            //     std::cout << "有新的连接到来了" << std::endl; 
                            // }
                        }
                        else
                        {
                            std::cout << "你有未读数据" << std::endl;
                            //正常读取
                            char buffer[1024];
                            ssize_t s = recv(sock, buffer, sizeof(buffer)-1, 0);
                            if(s > 0)
                            {
                                buffer[s] = 0;
                                std::cout << "client[" << sock << "]# " << buffer << std::endl;
                            }
                            else if(s == 0)
                            {
                                close(sock);
                                epoll_ctl(efd, EPOLL_CTL_DEL, sock, nullptr);
                                std::cout << "链接关闭了！" << std::endl;
                            }
                            else
                            {
                                close(sock);
                                epoll_ctl(efd, EPOLL_CTL_DEL, sock, nullptr);
                                std::cout << "读取失败！" << std::endl;
                            }
                        }
                    }
                    else if(evs[i].events & EPOLLOUT)
                    {
                        //写事件
                    }
                    else
                    {

                    }
                }
                break;
        }
    }

    return 0;
}
